Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

209
Vistas
lost with object programming : is my constructor function "conventional" ? is my code correctly factored in?

This question is more of a generic one. I'm starting with object programming and am quickly getting lost. It's working but I feel that I am only lucky that it is because my code is not clean...and it'll not be scalable

  1. can I leave this.rgb in the player construction function without defining it right away ? Would it be cleaner to create an "rgb function" that calculates the value (this.rgb = rgb)
  2. is it disgusting to define the rgb variable inside the newButton function (which is kind of supposed to be about something else)
  3. Do I really have to do this.newButton = newButton; (as seen on CDN) or is there a simpler/cleaner way ?
  4. Bonus : I read mike and marks explanations on templates but I feel like I can do things quicker (and dirtier) with this very long string defining the button... how dirty is it ?
/*------------------------------------------

        Player definition   
        
-------------------------------------------*/

function newButton() {
    
    r = Math.floor(Math.random() * 100 + 100)
    g = Math.floor(Math.random() * 100 + 100)
    b = Math.floor(Math.random() * 100 + 100)
    this.rgb = "rgb(" + r + ","  + g + "," + b + ")";
    
    return button = `<button onclick="playerTimer(${this.playerNumber})" class="btn" style="background-color:${this.rgb}"><h3>Player ${this.playerNumber+1} </h3> <br><br> Time left:<h2><div id="playerTimerDiv${this.playerNumber}"> 30 </div></h2></button>`;
}

function Player(playerNumber, timeout, sec) {
  this.playerNumber = playerNumber;
  this.rgb
  this.timeout = timeout;
  this.sec =sec;
  this.div = document.createElement("div");
  this.newButton = newButton;
}

Thank you very much in advance for your help ! Sorry for this very long question....

Best regards,

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda